package com.onstar.cn.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;

import com.onstar.cn.common.cache.ImageCache;

public class UnreadMarkImageView extends CustomImageView {
    private Context context;
    private Bitmap redmark;
    private Paint paint;
    private int bitmapWidth;
    private int bitmapHeight;
    private boolean showMark;
    private TypedArray typedArray;
    private float marginRight;
    private float marginTop;

    public UnreadMarkImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnreadMarkParam);
        initView();
    }

    public UnreadMarkImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UnreadMarkImageView(Context context) {
        this(context, null);
    }

    private void initView() {
        redmark = ((BitmapDrawable) ImageCache.getInstance(context).getCache(R.drawable.unread_mark, true)).getBitmap();
        bitmapWidth = redmark.getWidth();
        bitmapHeight = redmark.getHeight();
        paint = new Paint();
        marginRight = typedArray.getDimension(R.styleable.UnreadMarkParam_margin_right, 0);
        marginTop = typedArray.getDimension(R.styleable.UnreadMarkParam_margin_top, 0);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (showMark) {
            if (redmark == null) {
                initView();
            }
            canvas.drawBitmap(redmark, getWidth() - bitmapWidth - marginRight, marginTop, paint);
        }
    }

    public void showMark(boolean showMark) {
        this.showMark = showMark;
        invalidate();
    }

    public boolean isMarkShowing() {
        return showMark;
    }
}
